package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ReportQualificationMapper;
import com.hbnu.system.model.entity.Report;
import com.hbnu.system.model.entity.Report_Qualification;
import com.hbnu.system.model.entity.Report_Type;
import com.hbnu.system.service.IReportQualificationService;
import com.hbnu.system.service.IReportService;
import com.hbnu.system.service.IReportTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ReportQualificationServiceImpl extends ServiceImpl<ReportQualificationMapper, Report_Qualification> implements IReportQualificationService {
    @Autowired
    private IReportTypeService iReportTypeService;

    @Autowired
    private IReportService iReportService;

    @Override
    public boolean addReport_Qualification(Report_Qualification report_qualification) {
        Report report = iReportService.getReport(report_qualification.getReportID());
        if (Objects.isNull(report)){
            throw new BaseException("报告编号有误!");
        }
        if(Objects.nonNull(this.getOne(Wrappers.lambdaQuery(Report_Qualification.class)
                .eq(Report_Qualification::getReportID,report_qualification.getReportID())))){
            throw new BaseException("已经存在量化数据!");
        }
        if(String.valueOf(report_qualification.getType1ID()).equals("")){
            throw new BaseException("请完整填写数据!");
        }
        if (String.valueOf((report_qualification.getType2ID())).equals("")){
            throw new BaseException("请完整填写数据!");
        }

        Report_Type type = iReportTypeService.getOne(Wrappers.lambdaQuery(Report_Type.class)
                .eq(Report_Type::getType1ID, report_qualification.getType1ID())
                .eq(Report_Type::getType2ID, report_qualification.getType2ID()));

        report_qualification.setReportType1(type.getReportType1());
        report_qualification.setReportType2(type.getReportType2());
        report_qualification.setFinalValue(type.getValue());
        report.setScore(type.getValue());

        iReportService.updateById(report);

        return baseMapper.insert(report_qualification) == 1;
    }

    @Override
    public boolean deleteReport_Qualification(String Report_QualificationID) {
        Report_Qualification report_qualification = baseMapper.selectOne(Wrappers.lambdaQuery(Report_Qualification.class)
                .eq(Report_Qualification::getReportQualificationID, Report_QualificationID));

        if (Objects.isNull(report_qualification)){
            throw new BaseException("量化编号有误");
        }

        Report report = iReportService.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID, report_qualification.getReportID()));

        report.setScore(0);
        iReportService.updateById(report);


        return baseMapper.deleteById(Report_QualificationID) == 1;

    }

    @Override
    public List<Report_Qualification> getOneReport_Qualification(String ReportID) {
        Report report = iReportService.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID,ReportID));;
        if(Objects.isNull(report)){
            throw new BaseException("报告编号有误!");
        }

        List<Report_Qualification> report_qualifications = baseMapper.selectList(Wrappers.lambdaQuery(Report_Qualification.class)
                .eq(Report_Qualification::getReportID, ReportID));
        return report_qualifications;
    }
}
